Skip to content

Add polymorphic type#4

Open
dound wants to merge 4 commits intopocketgems:mainfrom
dound:add_polymorphic_type
Open

Add polymorphic type#4
dound wants to merge 4 commits intopocketgems:mainfrom
dound:add_polymorphic_type

Conversation

@dound
Copy link
Contributor

@dound dound commented Jan 5, 2024

Change Log

  • add polymorphic object type

Note this bumps the json schema version to the latest (2020). This will break the app package because fastify by default uses the older v7 json schema draft (in part because it's faster). That can be worked around as follows (this allows turns off type coercion for body elements because they should not need coercion, they should come with the correct types... but maybe some clients have been getting away with sending strings instead of the correct type so it'd be more backwards compatible to leave type coercion on for body too):

import Ajv from 'ajv/dist/2020'
...
const ajvObjs = {
  coerceTypes: new Ajv({ ...ajvParams, coerceTypes: true }),
  noCoerceTypes: new Ajv({ ...ajvParams, coerceTypes: false })
}
const app = fastify({...})
    .setValidatorCompiler(({ httpPart, schema }) => {
      // header, route, and query string always come as strings; coerce them
      // to the correct types if possible; body should come exactly in the
      // right type so don't allow type coercion there
      const useCoerce = httpPart !== 'body'
      const ajv = useCoerce ? ajvObjs.coerceTypes : ajvObjs.noCoerceTypes
      const validate = ajv.compile(schema)
      return (value) => !validate(value)
        ? ({ value, error: validate.errors })
        : ({ value })
    })   

Checklist

  • I confirm there is no private key, token, secret, etc. added in this pull request or any intermediate commits, as they will become publicly accessible and result in security breach.
  • By submitting this pull request to Pocket Gems' Github repository, I confirm that Pocket Gems can use, modify, copy and redistribute my contribution, under the terms of Pocket Gems' choice, which as of date of this submission, shall be the Apache License Version 2.0, and may be changed at any time at Pocket Gems' sole discretion.

dound added 4 commits January 4, 2024 20:58
uses if-then-else

requires json schema 2019-09 or newer so we can use unevaluatedProperties
add polymorphic type
@dound dound force-pushed the add_polymorphic_type branch from b11a049 to d17bb6c Compare January 5, 2024 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant